package com.hrzyit.whistle.cases.utils

import android.app.Activity
import android.net.Uri
import android.support.v4.app.ActivityCompat
import com.bigkoo.alertview.AlertView
import com.bigkoo.alertview.OnItemClickListener
import com.jph.takephoto.app.TakePhoto
import com.jph.takephoto.compress.CompressConfig
import java.io.File

/**
 * 上传文件(图片)弹出窗口
 * @author YangWei
 * @date 2019/4/16
 */
class TakePhotoAlertView(val activity: Activity) {

    /**
     * 弹出案件上传图片选择窗口
     */
    fun showCaseAddImageAlertView(takePhoto: TakePhoto,mTempFile: File) {
        AlertView(
            "上传照片",
            null,
            "取消",
            null,
            arrayOf("拍照", "从相册中选择"),
            activity,
            AlertView.Style.ActionSheet,
            OnItemClickListener { _, position ->
                ActivityCompat.requestPermissions(
                    activity,
                    arrayOf("android.permission.CAMERA"),
                    17
                )

                // 照片压缩，每次都要调用否则只会执行一次
                val config = CompressConfig.ofDefaultConfig()
                // TODO 2019/4/8 - 待完成内容：待确定压缩比 300k
                config.maxSize = 300 * 1024
                takePhoto.onEnableCompress(config, false)
                when (position) {
                    0 -> {
                        // 创建照片临时文件
                        createTempFile()
                        // 拍照
                        takePhoto.onPickFromCapture(Uri.fromFile(mTempFile))
                    }
                    1 -> {
                        takePhoto.onPickFromGallery()
                    }
                }
            }
        ).show()
    }

}